package com.garlic.face.jlf.transaction.balance;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;


import com.garlic.face.jlf.transaction.Means;
import com.garlic.face.jlf.transaction.constances.CommonConstant;
import com.garlic.face.jlf.utils.LogFileUtil;

import org.xutils.common.util.LogUtil;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by lyq on 2018/5/30.
 * Email: lyqcy1337@163.com
 */

public class CommonUtil {

    private static final String TAG = "CommonUtil";

    private static CommonUtil commonUtil = null;

    public static CommonUtil getInstance(){
        if (null == commonUtil) {
            synchronized (CommonUtil.class) {
                if (null == commonUtil) {
                    commonUtil = new CommonUtil();
                }
            }
        }
        return commonUtil;
    }

    private String shopNo = "";//店铺编号
    private String posNo = "";//站点编号
    private String transnmr = "";//当前订单号
    private int flowid = 0;//当前行号
    private String voidsIntervention = "";//0:取消单品/交易不需要授权  1:取消单品/交易需要授权
    private String[] bags = new String[]{"0", "0", "0"};//小中大购物袋
    // TODO: 2018/7/17 购物袋条码写死
    private String ipAdd = "";
    private String port = "";
    private String vipcard = "";


    public void setTransnmr(String transnmr) {
        this.transnmr = transnmr;
    }

    public String getTransnmr() {
        return transnmr;
    }

    public void setBags(String[] bags) {
        this.bags = bags;
    }

    public String[] getBags() {
        return bags;
    }

    public void setFlowid(int flowid) {
        this.flowid = flowid;
    }

    public int getFlowid() {
        return flowid;
    }

    public void setPosNo(String posNo) {
        this.posNo = posNo;
    }

    public String getPosNo() {
        return posNo;
    }

    public void setShopNo(String shopNo) {
        this.shopNo = shopNo;
    }

    public String getShopNo() {
        return shopNo;
    }

    public void setIpAdd(String ipAdd) {
        this.ipAdd = ipAdd;
    }

    public String getIpAdd() {
        return ipAdd;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getPort() {
        return port;
    }

    public void setVipcard(String vipcard) {
        this.vipcard = vipcard;
    }

    public String getVipcard() {
        return vipcard;
    }

    public String getVoidsIntervention() {
        return voidsIntervention;
    }

    public void setVoidsIntervention(String voidsIntervention) {
        this.voidsIntervention = voidsIntervention;
    }

    //private Store store; // 设置当前店铺
    private boolean isScanCode = true; // 是否允许扫码

    //public Store getStore() {
    //    return store;
    //}

    //public void setStore(Store store) {
    //    this.store = store;
    //}

    public boolean isScanCode() {
        return isScanCode;
    }

    public void setScanCode(boolean scanCode) {
        isScanCode = scanCode;
    }

    /**
     * 清除交易数据，首页处理
     */
    public void clearShopData(){
        Means.getInstance().clear();
    }

    /**
     * 获取设备唯一码
     * @return
     */
    public String getUUID(){
        return android.os.Build.SERIAL;
    }

    /**
     * 隐藏键盘
     * @param activity
     * @param editText
     */
    public void hideNumber(Activity activity, EditText editText){
        // 隐藏键盘
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
        editText.setInputType(0);
    }

    /**
     * 获取App自身版本
     *
     * @return
     */
    public static String getAppVersionCode(String packageName, PackageManager packageManager) {
        String versionCodeStr = "";
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
            String versionCode = packageInfo.versionName;
            versionCodeStr = String.valueOf(versionCode);
            LogFileUtil.writeLog(packageName + "自身版本：" + versionCode);
            LogUtil.d("onCreate: 自身版本" + versionCode);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return "0";
        }
        return versionCodeStr;
    }

    /**
     * 自更新
     *
     * @param uri
     */
    public static void installApk(Uri uri, Activity context) {
        //调用系统安装程序
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        context.startActivityForResult(intent, CommonConstant.START_MYSELF_REQUEST_CODE);
    }

    public static String[] parsingSupervisor(String str){
        if(str != null && str.length() > 0){
            int index = str.indexOf("$");
            if(index >= 0 && index + 1 < str.length()){
                String[] strings = new String[3];
                String key = Means.secretKey;
                String tmp = DesUtil.decryptPassword(str.substring(index + 1), key);
                if(tmp.contains("&") && tmp.contains("-")){
                    String date = tmp.substring(0, tmp.indexOf("&"));
                    SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
                    String dateStr = df.format(new Date());
                    if(date.equals(dateStr)){
                        String supervisor = tmp.substring(tmp.indexOf("&") + 1, tmp.indexOf("-"));
                        String supervisorPwd = tmp.substring(tmp.indexOf("-") + 1);
                        strings[0] = date;
                        strings[1] = supervisor;
                        strings[2] = supervisorPwd;
                        return strings;
                    }
                    else{
                        //权限卡过期
                        return null;
                    }
                }
                else {
                    return null;
                }
            }
            else{
                return null;//非法权限卡
            }
        }
        else {
            return null;
        }
    }
}
